package org.http4k.contract

import com.natpryce.hamkrest.assertion.assertThat
import com.natpryce.hamkrest.equalTo
import org.http4k.format.Json
import org.http4k.lens.Invalid
import org.http4k.lens.LensFailure
import org.http4k.lens.Meta
import org.http4k.lens.Missing
import org.http4k.lens.ParamMeta.ArrayParam
import org.http4k.lens.ParamMeta.NumberParam
import org.http4k.lens.ParamMeta.StringParam
import org.junit.jupiter.api.Test

abstract class JsonErrorResponseRendererContract<NODE : Any>(val j: Json<NODE>) {

    @Test
    fun `can build 400`() {
        val response = JsonErrorResponseRenderer(j).badRequest(LensFailure(listOf(
            Missing(Meta(true, "location1", ArrayParam(StringParam), "name1", null, emptyMap())),
            Invalid(Meta(false, "location2", NumberParam, "name2", null, emptyMap()))
        ), target = null
        )
        )
        assertThat(response.bodyString(),
            equalTo("""{"message":"Missing/invalid parameters","params":[{"name":"name1","type":"location1","datatype":"[string]","required":true,"reason":"Missing"},{"name":"name2","type":"location2","datatype":"number","required":false,"reason":"Invalid"}]}"""))
    }

    @Test
    fun `can build 404`() {
        val response = JsonErrorResponseRenderer(j).notFound()
        assertThat(response.bodyString(),
            equalTo("""{"message":"No route found on this path. Have you used the correct HTTP verb?"}"""))
    }
}
